package guyubao.com.controller;

import guyubao.com.entity.RestBean;
import guyubao.com.entity.dto.JDAccountDto;
import guyubao.com.entity.dto.JDApplyAccountDto;
import guyubao.com.entity.vo.request.JDApplyAccountVORequest;
import guyubao.com.service.JDAccountService;
import guyubao.com.service.JDApplyAccountService;
import guyubao.com.service.RedisService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.hibernate.validator.constraints.Length;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Validated
@RestController
@RequestMapping("/api/applyJDAccount")
// 开启事务管理
@EnableTransactionManagement
@Tag(name = "申请京东账号相关", description = "申请账号信息的增删改查")
public class JDApplyAccountController extends BaseController{
    @Resource
    JDApplyAccountService applyAccountService;
    @Resource
    JDAccountService jdAccountService;
    @Resource
    RedisService redisService;
    @Operation(summary ="根据id查询申请的京东账号")
    @GetMapping("/id")
    public RestBean<JDApplyAccountDto> getJDApplyAccountById(@RequestAttribute @Length(min=1,max = 20) String id){
        // 执行业务
        return RestBean.success(applyAccountService.getJDApplyAccountById(id));
    }
    @Operation(summary = "根据id修改申请的京东账号 修改state为1")
    @PutMapping("/id")
    public RestBean<Void> updateJDApplyAccountSubmit(@RequestBody @Valid JDApplyAccountVORequest vo,
                                                     @RequestAttribute String id,
                                                     @RequestAttribute String email){
        if (!id.equals(vo.getId()))return RestBean.failure(400,"提交的id与实际id不符！");
        // 获得邮箱
        if (email==null) return RestBean.failure(500,"出错了没有获得您的邮箱，请联系管理人员！");
        // 判断redis中的验证码是否对的上
        String verificationCode = redisService.verificationCode(vo.getCode(), "apply", email);
        if (verificationCode!=null)return RestBean.failure(400,verificationCode);
        // 查看数据库是否已有账号
        JDAccountDto jdAccountDto = jdAccountService.getJDAccountByPin(vo.getPt_pin());
        if (jdAccountDto!=null){
            // 查看该账号是否已经绑定
            if (loginMapperUserService.getIdByUId(jdAccountDto.getUId())!=null)
                return RestBean.forbidden("该账号已存在并被其他用户绑定！");
            // 没人绑定就该认领账号
            loginMapperUserService.addLoginMapperUser(id,jdAccountDto.getUId());
            // 修改申请账号的状态至完成
            applyAccountService.updateJDApplyAccountState(id,3);
            return RestBean.success();
        }
        // 执行申请业务
        return this.messageHandle(vo,applyAccountService::updateJDApplyAccountSubmit);
    }
    @Operation(summary = "根据id取消申请的京东账号 修改state为0")
    @DeleteMapping("/id")
    public RestBean<Void> updateJDApplyAccountCancel(@RequestAttribute String id){
        return this.messageHandle(()->
            applyAccountService.updateJDApplyAccountState(id, 0)
        );
    }
}
